<?php
include_once '\lib\calendar\calendar.inc.php';
include_once '\lib\FirePHPCore\fb.php';
include_once 'controller.php';
include_once '\lib\date\DateUtil.php';

class View{
	var $request;
	var $server;
	var $year;
	var $month;
	var $offset;
	var $controller;

	function View($request,$server){
		$this->year = date('Y');
		$this->month = date('m');
		$this->offset=2;
		$this->request = $request;
		$this->server  = $server;
		$this->controller = new Controller();
	}

	public function getOwnCalendar($userId){
		if($userId==0)
			return "<br><p style='color:red'>please log in</p>";
			
		if(isset($this->request['date'])) $date = $this->request['date'];
		if(isset($this->request['year'])) $this->year = $this->request['year'];
		if(isset($this->request['month'])) $this->month = $this->request['month'];
		if(isset($this->request['offset'])) $this->offset = $this->request['offset'];

		
		if(isset($this->server['PHP_SELF'])) $PHP_SELF = $this->server['PHP_SELF'];
		if(isset($this->request['page_id'])) $pageLink = $PHP_SELF.'?page_id='.$this->request['page_id'].'';
		else	$pageLink=$PHP_SELF.'?homepage=1';	
		$cal = new CALENDAR($this->year, $this->month);
		$cal->offset = $this->offset;
		$cal->customLink=true;
		$cal->link = $this->getHTMLLink('linkDescValue');
		
		
		$listaEventi = $this->controller->selectPerMonthAndUser($this->month,$this->year,$userId);
			
		for($i=0;$i<count($listaEventi);$i++){
			$item=$listaEventi[$i];
			$eventDate = DateUtil::extractDay($item['date']);
			$cal->viewEvent($eventDate,$eventDate,"#00FF00",$item['description'],'',true,$item['id']);
		}
			
		$html = $cal->create();
		$html.= $this->getMonthController($pageLink, $this->month, $this->year);
			
		if(isset($date)) $html = $html.'<p>You clicked ' . $date . '.';
		return $html;
	}

	public function modalPanelJQuery($userId){
		?>

	
		<div id="dialog" title="Insert Event or Update">
			<form id="modalForm" method="POST" action="#" enctype="multipart/form-data" >
				<fieldset>
					<input name="userId" type="hidden" value="<?php echo $userId?>">
					<input name="date" type="hidden" value="0">
					<input name="formAction" type="hidden" value="0">
					<input name="eventId" type="hidden" value="0">
					<label>Luogo</label>
					<input name="place" type="text" />
					<label>Descrizione</label>
					<textarea name="description" id="modalPanelDescription" rows="2" cols="20"></textarea><br />
					<label>Durata</label>
					<input name="workedHour" id="modalPanelWorkedHour" type="text" />
				</fieldset>
			</form>
		</div>
		<script type="text/javascript">
			jQuery(document).ready(function() {
		    	jQuery("#dialog").dialog({
				bgiframe: true, autoOpen: false, height: 200,width:400, modal: true,
				buttons: {
					'Add or Update': function() {
		    			jQuery("#dialog").dialog('close');
		    			jQuery("#modalForm").submit();
		    			return false;
						
					},
					Cancel: function() {
						jQuery("#dialog").dialog('close');
					},
					'Delete': function(){
						jQuery('[name=formAction]').val('2');	
						jQuery("#dialog").dialog('close');
						jQuery("#modalForm").submit();
						}
		    		}
	  			});
			});
		</script>
		<?php 
	}

	public function getHTMLLink($text){
		return "<a id=\"idDescValue\" href=\"#\" onclick=\"jQuery('[name=date]').val('idDescValue');
					jQuery('[name=formAction]').val('actionValue');
					jQuery('[name=eventId]').val('eventIdValue');												
					jQuery('#dialog').dialog('open'); 
					return false;\">$text</a>";
	}
	
	public function getMonthController($link,$month,$year){
		$month=(int) $month;
		$year = (int) $year;

		if($month==1){
			$previousMonth=12;
			$previousYear=$year-1;
		}	else{
			$previousMonth=$month-1;
			$previousYear=$year;
		}
			
		if($month==12){
			$nextMonth=1;
			$nextYear=$year+1;
		}	else{
			$nextMonth=$month+1;
			$nextYear=$year;
		}

		$html ="<div><div class='left'><a href='$link&month=$previousMonth&year=$previousYear'><< Previous Month</a></div>";
		$html.="<div class='right'><a href='$link&month=$nextMonth&year=$nextYear'> Next Month >></a></div></div>";
			
		return $html;
	}
}
?>